<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\goods\Order;
use app\common\controller\Api;
use app\common\controller\Payment;
use think\Controller;
use think\Db;
use think\Request;


class Userorders extends Api
{
    private static $model;
//    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\common\model\UserOrders();
    }

    /**
     * 开通会员
     */
    public function create()
    {
        $userId = $this->auth->id;
        $user = \app\common\model\User::find($userId);
        if ($user->level >= 2) {
            $this->error('你已是会员，无需开通！');
        }

        $vip_price = config('site.vip_price');
        if (!isset($vip_price) || !$vip_price) {
            $this->error('系统尚未配置会员价格！');
        }

        $orderNum = create_order($userId);
        $total = $vip_price;
        $odata = [
            'user_id' => $userId,
            'order_num' => $orderNum,
            'total' => $total,
            'status' => 0,
        ];

        Db::startTrans();
        try {
            $ret = self::$model->create($odata);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error('开通会员申请失败！');
        }

        $this->success('开通会员申请成功！');
    }

    /**
     * 订单支付
     */
    public function pay()
    {
        $userId = $this->auth->id;
        $orderNum = input('order_num');
        $row_info = self::$model->where('order_num', $orderNum)->find();
        if (!$row_info) {
            $this->error('参数错误！');
        }
        if ($row_info->status >= 1) {
            $this->error('该订单已支付！');
        }

        $user = \app\common\model\User::find($userId);
        if ($user->level >= 2) {
            $this->error('你已是会员，无需开通！');
        }

        $type = input('type', 2);//1小程序，2微信公众号，3h5，4app
        self::$model->where('id', $row_info->id)->update(['paytype' => $type]);
        Db::startTrans();
        try {
            $openIdApp = \app\common\model\User::where('id', $userId)->value('open_id_app');
            $openIdApplet = \app\common\model\User::where('id', $userId)->value('open_id_applet');

            $third_config = include(APP_PATH . 'third.php');
            $params = [
                'amount' => $row_info->total,
                'orderid' => $row_info->order_num,
                'type' => "wechat",
                'title' => '开通会员支付成功',
                'notifyurl' => $third_config['vip_notify_url'],
                'method' => "miniapp",
                'auth_code' => ""
            ];

            if ($type == 2) {
                $params['method'] = 'mp';
//                $params['openid'] = $openIdWechat;
            } elseif ($type == 1) {
                $params['method'] = 'miniapp';
                $params['openid'] = $openIdApplet;
            } elseif ($type == 3) {
                $params['method'] = 'app';
                $params['openid'] = $openIdApp;
            }

            $res = Service::submitOrder($params);

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('支付成功！', $res);
    }
}
